package com.pruebas.andEngine;

import org.anddev.andengine.engine.Engine;
import org.anddev.andengine.engine.camera.ZoomCamera;
import org.anddev.andengine.engine.options.EngineOptions;
import org.anddev.andengine.engine.options.EngineOptions.ScreenOrientation;
import org.anddev.andengine.engine.options.resolutionpolicy.RatioResolutionPolicy;
import org.anddev.andengine.entity.scene.Scene;
import org.anddev.andengine.entity.scene.background.SpriteBackground;
import org.anddev.andengine.entity.sprite.Sprite;
import org.anddev.andengine.entity.util.FPSLogger;
import org.anddev.andengine.input.touch.TouchEvent;
import org.anddev.andengine.opengl.texture.Texture;
import org.anddev.andengine.opengl.texture.TextureOptions;
import org.anddev.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlas;
import org.anddev.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlasTextureRegionFactory;
import org.anddev.andengine.opengl.texture.region.TextureRegion;
import org.anddev.andengine.ui.activity.BaseGameActivity;

import android.content.Intent;

public class Main extends BaseGameActivity {
	static final int CAMERA_WIDTH = 800;
	static final int CAMERA_HEIGHT = 480;
	public static final float MAX_CHARGE_DISTANCE = 80;

	private ZoomCamera mCamera;

	// aca cargo imagen de fondo
	private Texture mTexture;
	private TextureRegion mCajaTextureRegion;

	private BitmapTextureAtlas mBitmapTexturePlay;
	private BitmapTextureAtlas mBitmapHighScores;
	private BitmapTextureAtlas mBitmapHowToPlay;

	private TextureRegion jugar;
	private TextureRegion highScores;
	private TextureRegion howToPlay;

	private Intent intentJugar = null;
	private BaseGameActivity menuJugar = this;
	private Intent intentHighScores = null;
	private BaseGameActivity menuHighScores = this;
	private Intent intentHowToPlay = null;
	private BaseGameActivity menuHowToPlay = this;

	// SONIDOS
	// private Music musica;

	private Scene scene;

	@Override
	public Engine onLoadEngine() {
		this.mCamera = new ZoomCamera(0, 0, CAMERA_WIDTH, CAMERA_HEIGHT);
		return new Engine(new EngineOptions(true, ScreenOrientation.LANDSCAPE, new RatioResolutionPolicy(CAMERA_WIDTH, CAMERA_HEIGHT), this.mCamera));
	}

	@Override
	public void onLoadResources() {

		// en esta textura agrego la imagen de fondo
		this.mTexture = new BitmapTextureAtlas(1024, 1024, TextureOptions.BILINEAR_PREMULTIPLYALPHA);
		this.mCajaTextureRegion = BitmapTextureAtlasTextureRegionFactory.createFromAsset((BitmapTextureAtlas) this.mTexture, this, "gfx/background_sewage.png", 0, 0);

		// cargo la textura con la imagen de fondo
		this.mEngine.getTextureManager().loadTexture(this.mTexture);

		// agrego las tres opciones del menu
		this.mBitmapTexturePlay = new BitmapTextureAtlas(512, 512, TextureOptions.BILINEAR_PREMULTIPLYALPHA);
		this.jugar = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.mBitmapTexturePlay, this, "gfx/btn-jugar.png", 0, 0); // Jugar.png

		this.mBitmapHighScores = new BitmapTextureAtlas(512, 512, TextureOptions.BILINEAR_PREMULTIPLYALPHA);
		this.highScores = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.mBitmapHighScores, this, "gfx/btn-highscores.png", 0, 0); // HighScores.png

		this.mBitmapHowToPlay = new BitmapTextureAtlas(512, 512, TextureOptions.BILINEAR_PREMULTIPLYALPHA);
		this.howToPlay = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.mBitmapHowToPlay, this, "gfx/btn-comojugar.png", 0, 0); // ComoJugar.png

		this.mEngine.getTextureManager().loadTexture(this.mBitmapTexturePlay);
		this.mEngine.getTextureManager().loadTexture(this.mBitmapHighScores);
		this.mEngine.getTextureManager().loadTexture(this.mBitmapHowToPlay);
		// this.mEngine.getTextureManager().loadTexture(this.mBitmapExit);

		// carga musicca de fondo
		// try {
		// musica =
		// MusicFactory.createMusicFromAsset(this.mEngine.getMusicManager(),
		// this, "mfx/Airbrushed.mp3");
		// musica.setLooping(true);
		// } catch (IOException ioe) {
		// No se ha podido cargar el fichero. Realizar las operaciones
		// oportunas.
		// }

	}

	@Override
	public Scene onLoadScene() {
		this.mEngine.registerUpdateHandler(new FPSLogger());

		scene = new Scene(1);

		// aca agrego un sprite como background
		scene.setBackground(new SpriteBackground(new Sprite(0, 0, this.mCajaTextureRegion)));

		// cargo los sprites del menu
		final Sprite spriteJugar = new Sprite(30, 60, this.jugar) {
			@Override
			public boolean onAreaTouched(final TouchEvent pSceneTouchEvent, final float pTouchAreaLocalX, final float pTouchAreaLocalY) {
				intentJugar = new Intent(menuJugar, Jugar.class);
				onPause();
				startActivity(intentJugar);
				return true;
			}
		};

		final Sprite spriteHighScores = new Sprite(30, 180, this.highScores) {
			@Override
			public boolean onAreaTouched(final TouchEvent pSceneTouchEvent, final float pTouchAreaLocalX, final float pTouchAreaLocalY) {
				intentHighScores = new Intent(menuHighScores, HighScores.class);
				onPause();
				startActivity(intentHighScores);
				return true;
			}
		};

		final Sprite spriteHowToPlay = new Sprite(30, 300, this.howToPlay) {
			@Override
			public boolean onAreaTouched(final TouchEvent pSceneTouchEvent, final float pTouchAreaLocalX, final float pTouchAreaLocalY) {
				intentHowToPlay = new Intent(menuHowToPlay, HowToPlay.class);
				onPause();
				startActivity(intentHowToPlay);
				return true;
			}
		};

		scene.attachChild(spriteJugar);
		scene.attachChild(spriteHighScores);
		scene.attachChild(spriteHowToPlay);

		scene.registerTouchArea(spriteJugar);
		scene.registerTouchArea(spriteHighScores);
		scene.registerTouchArea(spriteHowToPlay);

		return scene;
	}

	@Override
	public void onLoadComplete() {
		// this.musica.setVolume(0,1);
		// this.musica.play();
		// TODO Auto-generated method stub
	}

	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		// ACA DEBERIAMOS PAUSAR LA MUSICA
		super.onPause();
	}

}